import Vue from 'vue'
import App from './App'
import router from './router'
import store from './store'
import iView from 'view-design'
import i18n from '@/locale'
import config from '@/config'
import importDirective from '@/directive'
import { directive as clickOutside } from 'v-click-outside-x'
import checkPermission from '@/libs/checkPermission.js'
import { getToken } from '@/libs/util'
import easyAjax from '@/api/request'
import './index.less'
import '@/assets/icons/iconfont.css'

Vue.use(iView, {
  i18n: (key, value) => i18n.t(key, value)
})
Vue.use(checkPermission)
/**
 * @description 生产环境关掉提示
 */
Vue.config.productionTip = false
/**
 * @description 全局注册应用配置
 */
Vue.prototype.$config = config
/**
 * @description 全局AJAX方法
 */
Vue.prototype.$easyAjax = easyAjax
/**
 * 注册指令
 */
importDirective(Vue)
Vue.directive('clickOutside', clickOutside)

window.addEventListener('beforeunload', e => {
  iView.Spin.show()
})

/**
 * 如果路由为空则先获取路由
 */
if (getToken()) {
  /**
   * 解决动态路由刷新产生的白屏
   */
  if ((store.state.app.routers).length <= 0) {
    store.dispatch('getUserInfo')
    iView.Spin.show()
    store.dispatch('getRouters').then(() => {
      iView.Spin.hide()
      let vm = new Vue({ el: '#app', router, i18n, store, render: h => h(App) })
      Vue.use({ vm })
    })
  }
} else {
  let vm = new Vue({ el: '#app', router, i18n, store, render: h => h(App) })
  Vue.use({ vm })
}
